import os
import scrapy

"""
参考：https://vbni1d7bly.feishu.cn/wiki/FO6CwzrR2iVVm4kcJeGcntGGnhg?fromScene=spaceOverview
"""
class ItyouknowSpider(scrapy.Spider):
    name = 'ityouknow'
    allowed_domains = ['www.ityouknow.com']
    start_urls = ['http://www.ityouknow.com/spring-boot.html']

    def __init__(self):
        super(ItyouknowSpider, self).__init__()
        self.parse_items = []

    def parse(self, response):
        articles = response.css('a.posts-list-name.bubble-float-left')
        for article in articles:
            article_name = article.css('::text').get().strip()
            article_link = article.css('::attr(href)').get()
            self.parse_items.append({
                'article_name': article_name,
                'article_link': article_link
            })
            yield {
                'article_name': article_name,
                'article_link': article_link
            }

    def closed(self, reason):
        # 删除已存在的文件
        file_path = 'E:/tmp/articles.csv'  # 写死的文件路径
        if os.path.exists(file_path):
            os.remove(file_path)

        # 保存结果到指定路径
        try:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write('article_name,article_link\n')
                for item in self.parse_items:
                    f.write(f"{item['article_name']},{item['article_link']}\n")
            print("执行成功")
        except Exception as e:
            print(f"执行失败：{e}")
